package org.wisigoth.chat.client.jabber.xml;

import java.io.FilterReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class XercesReader extends FilterReader {

  public XercesReader(InputStream in){
    super(new InputStreamReader(in));
  }

  private XercesReader(Reader in){
    super(in);
  }
  int sendBlank = 0;

  @Override
public int read() throws IOException {
    if (this.sendBlank > 0) {
      this.sendBlank--;
      return ' ';
    }
    int b = this.in.read();
    if (b == '>'){
      this.sendBlank = 2;
    }
    return b;
  }

  @Override
public int read(char [] text, int offset, int length) throws IOException {
    int numRead = 0;
    for (int i = offset; i < offset + length; i++){
      int temp = this.read();
      if (temp == -1) break;
      text[i] = (char) temp;
      numRead++;
    }
    if (numRead == 0 && length != 0) numRead = -1;
    return numRead;
  }

}